﻿#include "submitproductwidget.h"
#include "ui_submitproductwidget.h"

SubmitProductWidget::SubmitProductWidget(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SubmitProductWidget)
{
    ui->setupUi(this);

    connect(&productService, &ProductService::addProductSuccess, this, &SubmitProductWidget::clear);
}

SubmitProductWidget::~SubmitProductWidget()
{
    delete ui;
}

void SubmitProductWidget::setWidgetType(SubmitWidgetType widgetType)
{
    this->widgetType = widgetType;

    setWindowTitle(widgetType==SubmitWidgetType::NewProductWidget?"新商品":"编辑商品");
    ui->pushButtonDelete->setVisible(widgetType==SubmitWidgetType::UpdateProductWidget);

    setTabOrder(ui->lineEditProductType, ui->lineEditProductName);
    setTabOrder(ui->lineEditProductName, ui->lineEditUnit);
    setTabOrder(ui->lineEditUnit, ui->lineEditProductNum);
    setTabOrder(ui->lineEditProductNum, ui->lineEditUpperLimit);
    setTabOrder(ui->lineEditUpperLimit, ui->lineEditLowerLimit);
    setTabOrder(ui->lineEditLowerLimit, ui->lineEditBrand);
    setTabOrder(ui->lineEditBrand, ui->lineEditOriginPlace);
    setTabOrder(ui->lineEditOriginPlace, ui->lineEditSpecifications);
    setTabOrder(ui->lineEditSpecifications, ui->lineEditBarCode);
    setTabOrder(ui->lineEditBarCode, ui->lineEditRemark);
    setTabOrder(ui->lineEditRemark, ui->lineEditGuidePrice1);
    setTabOrder(ui->lineEditGuidePrice1, ui->lineEditGuidePrice2);
    setTabOrder(ui->lineEditGuidePrice2, ui->comboBoxPrimaryDirectory);
    setTabOrder(ui->comboBoxPrimaryDirectory, ui->comboBoxSecondaryDirectory);
}

void SubmitProductWidget::setDirectory(QStringList primaryDirectory, QList<QStringList> secondaryDirectory)
{
    this->primaryDirectoryList = primaryDirectory;
    this->secondaryDirectoryList = secondaryDirectory;
    ui->comboBoxPrimaryDirectory->clear();
    ui->comboBoxPrimaryDirectory->addItems(primaryDirectoryList);
}

void SubmitProductWidget::setCurrentDirectory(QString primaryDirectory, QString secondaryDirectory)
{
    ui->comboBoxPrimaryDirectory->setCurrentText(primaryDirectory);
    ui->comboBoxSecondaryDirectory->setCurrentText(secondaryDirectory);
}

void SubmitProductWidget::on_pushButtonCancel_clicked()
{
    close();
}

void SubmitProductWidget::showAndClear()
{
    clear();
    exec();
}

void SubmitProductWidget::clear(){
    ui->lineEditProductType->clear();
    ui->lineEditProductName->clear();
    ui->lineEditUnit->clear();
    ui->lineEditProductNum->clear();
    ui->lineEditUpperLimit->clear();
    ui->lineEditLowerLimit->clear();
    ui->lineEditBrand->clear();
    ui->lineEditOriginPlace->clear();
    ui->lineEditSpecifications->clear();
    ui->lineEditBarCode->clear();
    ui->lineEditRemark->clear();
    ui->lineEditGuidePrice1->clear();
    ui->lineEditGuidePrice2->clear();
}

void SubmitProductWidget::initWithProduct(Product *product)
{
    this->product = product;
    ui->lineEditProductType->setText(product->type);
    ui->lineEditProductName->setText(product->name);
    ui->lineEditUnit->setText(product->unit);
    ui->lineEditProductNum->setText(product->productNum);
    ui->lineEditUpperLimit->setText(QString("%1").arg(product->upperLimit));
    ui->lineEditLowerLimit->setText(QString("%1").arg(product->lowerLimit));
    ui->lineEditBrand->setText(product->brand);
    ui->lineEditOriginPlace->setText(product->originPlace);
    ui->lineEditSpecifications->setText(product->specifications);
    ui->lineEditBarCode->setText(product->barCode);
    ui->lineEditRemark->setText(product->remark);
    ui->lineEditGuidePrice1->setText(QString("%1").arg(product->guidePrice1));
    ui->lineEditGuidePrice2->setText(QString("%1").arg(product->guidePrice2));
    ui->comboBoxPrimaryDirectory->setCurrentText(product->primaryDirectory);
    ui->comboBoxSecondaryDirectory->setCurrentText(product->secondaryDirectory);
    ui->pushButtonDelete->setVisible(
                product->count==0 && product->sanbao==0 && widgetType==SubmitWidgetType::UpdateProductWidget);
}

void SubmitProductWidget::showWithTargetProduct(Product *product){
    clear();
    ui->lineEditProductType->setText(product==nullptr?"":product->type);
    ui->lineEditProductName->setText(product==nullptr?"":product->name);
    exec();
}

void SubmitProductWidget::on_pushButtonSubmit_clicked()
{   
    if(widgetType == SubmitWidgetType::NewProductWidget){
        productService.addProduct(
                    this,
                    ui->pushButtonSubmit,
                    ui->lineEditProductType->text(),
                    ui->lineEditProductName->text(),
                    ui->lineEditUnit->text(),
                    ui->lineEditProductNum->text(),
                    ui->lineEditUpperLimit->text(),
                    ui->lineEditLowerLimit->text(),
                    ui->lineEditBrand->text(),
                    ui->lineEditOriginPlace->text(),
                    ui->lineEditSpecifications->text(),
                    ui->lineEditBarCode->text(),
                    ui->lineEditRemark->text(),
                    ui->lineEditGuidePrice1->text(),
                    ui->lineEditGuidePrice2->text(),
                    ui->comboBoxPrimaryDirectory->currentText(),
                    ui->comboBoxSecondaryDirectory->currentText());
    }else if(widgetType == SubmitWidgetType::UpdateProductWidget){
        productService.updateProduct(
                    this,
                    product->productId,
                    ui->lineEditProductType->text(),
                    ui->lineEditProductName->text(),
                    ui->lineEditUnit->text(),
                    ui->lineEditProductNum->text(),
                    ui->lineEditUpperLimit->text(),
                    ui->lineEditLowerLimit->text(),
                    ui->lineEditBrand->text(),
                    ui->lineEditOriginPlace->text(),
                    ui->lineEditSpecifications->text(),
                    ui->lineEditBarCode->text(),
                    ui->lineEditRemark->text(),
                    ui->lineEditGuidePrice1->text(),
                    ui->lineEditGuidePrice2->text(),
                    ui->comboBoxPrimaryDirectory->currentText(),
                    ui->comboBoxSecondaryDirectory->currentText());
    }
}

void SubmitProductWidget::on_comboBoxPrimaryDirectory_currentIndexChanged(int index)
{
    if(index !=-1)
    {
        ui->comboBoxSecondaryDirectory->clear();
        ui->comboBoxSecondaryDirectory->addItems(secondaryDirectoryList[index]);
    }
}

void SubmitProductWidget::on_pushButtonDelete_clicked()
{
    productService.deleteProduct(this, product);
}
